package lab01;

public class Test05 {
    public static void main(String[] args) {
        int n = 100;
        System.out.println("for循环结果=" + sum1(n));
        System.out.println("while循环结果=" +sum2(n));
        System.out.println("do while循环结果="+sum3(n));
    }
    //for循环求和
    public static int sum1(int n) {
        int i = 1;
        int sum = 0;
        for (i = 1; i <= n; i++) {
            sum += i;
        }
        return sum;
    }
    //while循环求和
    public static int sum2(int n) {
        int i = 1;
        int sum = 0;
        while (i <= n) {
            sum += i;
            i++;
        }
        return sum;
    }
    //do while循环求和*/
    public static int sum3(int n) {
        int i = 1;
        int sum = 0;
        do {
            sum += i;
            i++;
        }
        while (i <= n);
        return sum;
    }
}
